package nbn.android.myyoutubeclient.view;

import nbn.android.myyoutubeclient.R;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class AdmobView extends RelativeLayout {
	// private LayoutInflater layoutInflater;
	// RelativeLayout mActionLayout;
	Context context;
	private AdView adView;
	  private static final String LOG_TAG = "AdmobView";
	public AdmobView(final Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		adView = new AdView(context);
		adView.setAdSize(AdSize.BANNER);
		adView.setAdUnitId(context.getString(R.string.admob_id_banner));
		
		adView.setAdListener(new AdListener() {
		      /** Called when an ad is clicked and about to return to the application. */
		      @Override
		      public void onAdClosed() {
		        Log.d(LOG_TAG, "onAdClosed");
		      }

		      /** Called when an ad failed to load. */
		      @Override
		      public void onAdFailedToLoad(int error) {
		        String message = "onAdFailedToLoad: " + getErrorReason(error);
		        Log.d(LOG_TAG, message);
		      }

		      /**
		       * Called when an ad is clicked and going to start a new Activity that will
		       * leave the application (e.g. breaking out to the Browser or Maps
		       * application).
		       */
		      @Override
		      public void onAdLeftApplication() {
		        Log.d(LOG_TAG, "onAdLeftApplication");
		      }

		      /**
		       * Called when an Activity is created in front of the app (e.g. an
		       * interstitial is shown, or an ad is clicked and launches a new Activity).
		       */
		      @Override
		      public void onAdOpened() {
		        Log.d(LOG_TAG, "onAdOpened");
		      }

		      /** Called when an ad is loaded. */
		      @Override
		      public void onAdLoaded() {
		        Log.d(LOG_TAG, "onAdLoaded");
		        setVisibility(View.VISIBLE);
		      }
		    });    
		setGravity(Gravity.CENTER);
		addView(adView);

		AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
		// .addTestDevice("1B2F7C785555B0DDD2E6D5ECA46417FF")
		.build();

		// Start loading the ad in the background.
		adView.loadAd(adRequest);
	}
	/** Gets a string error reason from an error code. */
	  private String getErrorReason(int errorCode) {
	    String errorReason = "";
	    switch(errorCode) {
	      case AdRequest.ERROR_CODE_INTERNAL_ERROR:
	        errorReason = "Internal error";
	        break;
	      case AdRequest.ERROR_CODE_INVALID_REQUEST:
	        errorReason = "Invalid request";
	        break;
	      case AdRequest.ERROR_CODE_NETWORK_ERROR:
	        errorReason = "Network Error";
	        break;
	      case AdRequest.ERROR_CODE_NO_FILL:
	        errorReason = "No fill";
	        break;
	    }
	    return errorReason;
	  }
}
